﻿Imports System.Data.SqlClient
Imports System.Configuration
Public Class FmNewNhaCC
    Dim DA As New SqlDataAdapter
    Dim t As New DataTable

    Private Sub FmNewNhaCC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TxtMaNCC.Text = MaxMaNCC()

        Dim dieuphoi As New SqlDataAdapter("Select * from NHA_CC", KetNoi)

        Dim DTKH As New DataTable
        dieuphoi.Fill(DTKH)
        KetNoi.Close()
        DgvMaNCC.DataSource = DTKH

        BindingSource1.DataSource = DTKH
        DgvMaNCC.DataSource = BindingSource1

        DgvMaNCC.Visible = False

        'CbChonMl.DataSource = DTKH
        'CbChonMl.DisplayMember = "MALOAI"
    End Sub
    Public Function MaxMaNCC() As String
        Dim SOMAX As Long
        Dim sql4 As String
        'Dim conn4 As SqlConnection = New SqlConnection(CONSTR)
        'conn4.Open()
        XdKetNoi()

        Dim Cmd As SqlCommand = New SqlCommand()
        Dim prm As SqlParameter = New SqlParameter()
        sql4 = "select max(CAST(RIGHT(MACC,2) AS INT)) AS SOMAX from NHA_CC"
        Cmd.Connection = KetNoi
        Cmd.CommandType = CommandType.Text
        Cmd.CommandText = sql4
        SOMAX = Cmd.ExecuteScalar()
        Dim stt As String
        'stt = "001"
        'Dim SOMAX As Integer = CType(Cmd.ExecuteScalar().ToString, Integer) + 1
        If SOMAX < 10 Then
            stt = "0" + CType(SOMAX + 1, String)
        ElseIf SOMAX >= 10 And SOMAX < 100 Then
            stt = "0" + CType(SOMAX + 1, String)
        Else
            stt = CType(SOMAX + 1, String)
        End If
        MaxMaNCC = "CC" + stt
    End Function
    Public Sub ThemNCC()
        Dim Result As Integer

        Dim sql4 As String
        XdKetNoi()

        Dim Cmd As SqlCommand = New SqlCommand()
        Dim prm As SqlParameter = New SqlParameter()
        sql4 = "insert into NHA_CC (MACC,TENCC,DIACHI,SDTHOAI,TINH,MSTHUE,STKHOAN) VALUES('" & TxtMaNCC.Text & "', '" & TxtTenNCC.Text & "', '" & TxtDiaChi.Text & "', '" & TxtSDT.Text & "', '" & TxtMaNCC.Text & "', '" & TxtMST.Text & "' ,'" & TxtSoTK.Text & "')"
        Cmd.Connection = KetNoi
        Cmd.CommandType = CommandType.Text
        Cmd.CommandText = sql4
        Try
            Result = Cmd.ExecuteNonQuery()
            MessageBox.Show("Thêm thành công")
            Me.Close()




        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BtThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtThem.Click
        If TxtSoTK.Text <> "" And TxtTenNCC.Text <> "" And TxtDiaChi.Text <> "" And TxtSDT.Text <> "" And TxtMST.Text <> "" And TxtTinh.Text <> "" Then
            ThemNCC()
        Else
            MsgBox("không đủ thông tin")
        End If
    End Sub

    Private Sub BtHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtHuy.Click
        TxtDiaChi.Clear()
        TxtMaNCC.Clear()
        TxtMST.Clear()
        TxtSDT.Clear()
        TxtSoTK.Clear()
        TxtTenNCC.Clear()
        TxtTinh.Clear()
    End Sub

    Private Sub BtThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtThoat.Click
        Me.Close()
    End Sub
End Class